﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using BidFunLib.DAO;
using BidFunLib.Entity;

namespace BidFunLib.Services
{
    /// <summary>
    /// 
    /// </summary>
    public class DayManager
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(DayManager));

        /// <summary>
        /// Type allowed for searching BidOperatorTypeById
        /// </summary>
        public enum DAYS { LUNDI, MARDI, MERCREDI, JEUDI, VENDREDI, SAMEDI, DIMANCHE}

        /// <summary>
        /// The dao of this manager
        /// </summary>
        private DayDAO dao = new DayDAO();

        /// <summary>
        /// 
        /// </summary>
        public List<Day> ListDay { get; set; }

        /// <summary>
        /// Singleton instance
        /// </summary>
        private static DayManager instance;

        /// <summary>
        /// Retreive singleton of this class
        /// </summary>
        public static DayManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new DayManager();
                }
                return DayManager.instance;
            }
            set
            {
                DayManager.instance = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private DayManager()
        {
               ListDay = new List<Day>();
        }

        /// <summary>
        /// Retourne le day correspondant à la date.
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public Day getDayByDate(DateTime date)
        {
            return getDayByLabel("Mardi");
        }

        /// <summary>
        /// Get the day corresponding to the french label.
        /// Convert string to lowerString to compare
        /// Looks first in local list.
        /// If day is not present get it from database.
        /// If still not present => save it in DB.
        /// </summary>
        /// <param name="label"></param>
        /// <returns></returns>
        public Day getDayByLabel(String label)
        {
            // search in memory list
            foreach (Day day in ListDay)
            {
                if (day.DayLabel.ToLower() == label.ToLower())
                {
                    return day;
                }
            }

            // not found so search in database or create if not exist
            Day newDay = retreiveOrCreateDayByLabel(label);
            ListDay.Add(newDay);
            return newDay;
        }


        /// <summary>
        /// Delegate to DAO the management of the user search or creation
        /// </summary>
        /// <param name="pseudo"></param>
        /// <returns></returns>
        private Day retreiveOrCreateDayByLabel(String label)
        {
            Day day = null;

            // first of all we search for pseudo in DB
            day = dao.getDayByLabel(label);

            // not found so we save a new one
            if (day == null)
            {
                log.Debug("Day " + label + " must be created and saved into database.");
                day = new Day();
                day.DayLabel = label;

                dao.saveDay(day);

                day = dao.getDayByLabel(label);
            }

            return day;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="enumDay"></param>
        /// <returns></returns>
        public Day getDayByEnum(BidFunLib.Services.DayManager.DAYS enumDay)
        {
            // search in memory list
            foreach (Day day in ListDay)
            {
                if (day.DayLabel == enumDay.ToString())
                {
                    log.Debug("User " + day.DayLabel + " already exist in memory.");
                    return day;
                }
            }

            // not found so search in database or create if not exist
            Day newDay = dao.getDayByEnum(enumDay);
            ListDay.Add(newDay);
            return newDay;
        }

        /// <summary>
        /// Get the day corresponding to the french label.
        /// Convert string to lowerString to compare
        /// Looks first in local list.
        /// If day is not present get it from database.
        /// </summary>
        /// <param name="label"></param>
        /// <returns></returns>
        public Day getDayByID(Int32 id)
        {
            // search in memory list
            foreach (Day day in ListDay)
            {
                if (day.IdDay == id)
                {
                    log.Debug("User " + day.DayLabel + " already exist in memory.");
                    return day;
                }
            }

            // not found so search in database or create if not exist
            Day newDay = dao.getDayByID(id);
            ListDay.Add(newDay);
            return newDay;
        }

    }
}
